package com.example.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springbootblog.common.exception.BlogException;
import com.example.springbootblog.common.pojo.dataobject.BlogInfo;
import com.example.springbootblog.common.pojo.dataobject.UserInfo;
import com.example.springbootblog.common.pojo.request.RegisterInfoParam;
import com.example.springbootblog.common.pojo.request.UserLoginParam;
import com.example.springbootblog.common.pojo.response.UserInfoResponse;
import com.example.springbootblog.common.pojo.response.UserLoginResponse;
import com.example.springbootblog.common.utils.BeanConver;
import com.example.springbootblog.common.utils.JwtUtil;
import com.example.springbootblog.common.utils.SecurityUtil;
import com.example.springbootblog.mapper.BlogInfoMapper;
import com.example.springbootblog.mapper.UserInfoMapper;
import com.example.springbootblog.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource(name = "userInfoMapper")
    UserInfoMapper userInfoMapper;

    @Resource(name = "blogInfoMapper")
    BlogInfoMapper blogInfoMapper;




    @Override
    public UserLoginResponse login(UserLoginParam param) {
//        获取到该用户名的信息
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, param.getUserName())
                .eq(UserInfo::getDeleteFlag, 0));
//        校验返回的用户名是否为null
        if (userInfo == null) {
            throw new BlogException("用户名错误!");
        }

//        开始校验密码
        boolean b = SecurityUtil.checkCiphertext(param.getPassword(), userInfo.getSafePassword());
        if (!b) {
            throw new BlogException("密码错误!");
        }

//        开始设置登录效应
        UserLoginResponse response = new UserLoginResponse();
        response.setUserId(userInfo.getId());

        /**
            生成token
            1. 使用 map 设置内核
            2. 加入map 得到令牌
            3. 讲令牌加入到用户登录响应数据
         */

        Map<String,Object> map = new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",userInfo.getUserName());
        String token = JwtUtil.genToken(map);

//        设置 token
        response.setToken(token);
        return response;
    }


    /**
     * 效应列表页面的作者信息
     * @param userId
     * @return
     */


    @Override
    public UserInfoResponse getUserInfoById(Integer userId) {

        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId, userId)
                .eq(UserInfo::getDeleteFlag, 0));
//        查询失败
        if (userInfo == null) {
            return null;
        }
//        转化
        UserInfoResponse trans = BeanConver.trans(userInfo);


        // 填充文章总数和用户人数
        Long aLong = userInfoMapper.selectCount(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getDeleteFlag, 0));
        Long bLog = blogInfoMapper.selectCount(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, 0));

        log.info("总用户人数:{}",aLong);
        trans.setCount(aLong);
        trans.setSum(bLog);

        return trans;
    }

    /**
     * 效应文章详情页的作者信息
     * @param blogId
     * @return
     */
    @Override
    public UserInfoResponse getAuthorInfoById(Integer blogId) {
//        获取到博客的全部信息
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));

//        判断获取是否有效
        if (blogInfo == null) {
            return null;
        }
//        调用回去用户信息的方法
//        从而获取到作者信息
//        直接返回
        return  getUserInfoById(blogInfo.getUserId());
    }

    @Override
    public boolean registerUser(RegisterInfoParam userData) {
//        数据转化
        UserInfo userInfo = BeanConver.trans(userData);

        // 生成密文
        String securityPassword = SecurityUtil.genCiphertext(userData.getPassword());
        userInfo.setSafePassword(securityPassword);

//        进行插入
        int insert = userInfoMapper.insert(userInfo);
//        插入失败
        if (insert != 1) {
            return false;
        }


        return true;
    }
}
